/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <stdio.h>
#include <sys/time.h>
#include <linux/limits.h>

#define TIMESTAMP_MAX_LEN                33
#define LOG_LEVEL_NAME_MAX_LEN           10
#define LOG_BUF_MAX_LEN                  1024
#define QT_ENCLAVE_BIN                   "qt-enclave"
#define QT_ENCLAVE_LOG_FILE              "/var/log/qingtian_enclaves/qingtian-tool.log"
#define QT_ENCLAVE_LOG_SIZE              (2 * 1024 * 1024)
#define QT_ENCLAVE_LOG_MAX_CNT           9
// 2022-07-26T20:37:00 |info|qt-enclave[50]|func[157]|: xxx
#define QT_ENCLAVE_LOG_FORMAT            "%s|%s|%s[%d]|%s[%d]|: %s"
#define QT_ENCLAVE_LOG_ROLLER_LOCK       "/var/run/enclave/qt_roller.lock"
#define QT_ENCLAVE_ROLLER_LOCK_TIMEOUT   60
#define QT_ENCLAVE_ROLLER_LOCK_RATIO     100
#define QT_ENCLAVE_ROLLER_SLEEPTIME      10000
#define DEFAULT_SECFRACPRECISION         6
#define BEGIN_YEAR                       1900
#define QT_ENCLAVE_ERR_LEN               (2 * 1024)

typedef enum LOG_LEVEL {
    LOG_INFO,
    LOG_DEBUG,
    LOG_ERR,
    LOG_WARNING
} level_t;

typedef struct logger_level_s {
    char *level_name;
    level_t level;
} logger_level_t;

typedef struct timestamp_s {
    short year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    int secfracPrecision;
    int secfrac;
    char offsetMode;
    int offsetHour;
    int offsetMinute;
} timestamp_t;

typedef struct roller_s {
    char roller_lock[PATH_MAX + 1];
    int roller_lock_fd;
    int lock_timeout;
    int max_cnt;
    unsigned int size_limit;
} roller_t;

typedef struct logger_s {
    char logger_file[PATH_MAX - 1];
    FILE *logfile_fd;
    roller_t *roller;
} logger_t;

typedef struct event_s {
    char timestamp[TIMESTAMP_MAX_LEN];
    char level_name[LOG_LEVEL_NAME_MAX_LEN];
    char buf[LOG_BUF_MAX_LEN + 1];
} event_t;

void enclave_log(int level, const char *funcname, int linenr, const char *fmt, ...);
int logger_init();
void logger_del();